package com.example.demo.service;

import com.example.demo.entity.domain.Teacher;
import com.example.demo.entity.enums.CodeEnum;
import com.example.demo.respository.TeacherRepository;
import com.example.demo.vo.QueryObj;
import com.example.exceptions.RException;
import org.aspectj.apache.bcel.classfile.Code;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;



@Service
public class TeacherService {

    @Resource
    private TeacherRepository teacherRepository;

    // 查询所有的教师
    public List<Teacher> findAllTeacher(){
        return this.teacherRepository.findAll();
    }

    /**
     * 根据id 查询教师
     * @param id
     * @return
     */
    public Teacher findTeacherById(Integer id){
        return this.teacherRepository.findById(id).orElse(null);
    }


    /**
     * 根据学工号 密码查询
     * @param sn
     * @param password
     * @return
     */
    public Teacher findBySnAndPassword(String sn,String password){
       return this.teacherRepository.findBySnAndPassword(sn,password);
    }


    /**
     * 根据学工号密码查询
     * @param phone
     * @param password
     * @return
     */
    public Teacher findByPhoneAndPassword(String phone,String password){
        return this.teacherRepository.findByPhoneAndPassword(phone,password);
    }


    /**
     * 保存教师信息
     * @param teacher
     */
    public void saveTeacher(Teacher teacher){
        this.teacherRepository.save(teacher);
    }

    /**
     * 修改教师信息
     * @param teacher
     */
    public void updateTeacher(Teacher teacher){
        this.teacherRepository.save(teacher);
    }


    /**
     * 删除教师信息
     */
    public void deleteTeacher(Teacher teacher){
        this.teacherRepository.delete(teacher);
    }


    public List<Teacher> findMaxSn(){
        return this.teacherRepository.findMaxSn();
    }


    /**
     * 根据姓名查询
     * @param name
     * @return
     */
    public List<Teacher> findByNameLike(String name){
        return this.teacherRepository.findByNameLike(name);
    }

    public  void saveInfo(Teacher teacher) {
        this.teacherRepository.save(teacher);
    }


    public Teacher validateNameAndPassword(String sn,String password){
        List<Teacher> teacherList = teacherRepository.findBySn(sn);
        if (teacherList.size()<=0){
            throw new RException(CodeEnum.LOGIN_ERROR);
        }
        Teacher teacher = teacherList.get(0);
        if (!teacher.getPassword().equals(password)){
            throw new RException(CodeEnum.LOGIN_ERROR);
        }
        return teacher;
    }


    //分页查询不含查询参数
    public Page<Teacher> getAll(Pageable pageable){
        return teacherRepository.findAll(pageable);
    }

    //分页查询 含有查询参数
    public Page<Teacher> getAll(Example<Teacher> example,Pageable pageable){
        return teacherRepository.findAll(example,pageable);
    }


    /**
     * 分页查询
     * @param query
     * @return
     */
    public Page<Teacher> findData(QueryObj<Teacher> query) {
        // 排序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");

        if (query.getPage()==null){
            query.setPage(1);
        }

        if (query.getLimit()==null){
            query.setLimit(15);
        }

        // 传递参数查询
        Pageable pageable=PageRequest.of(query.getPage(),query.getLimit(),sort);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnoreNullValues();
        Example<Teacher> example=Example.of(query.getData(),matcher);
        return  teacherRepository.findAll(example,pageable);
    }

    /**
     * 批量删除信息
     * @param ids
     */
    @Transactional(rollbackOn = Exception.class)
    public void delDtaListBatch(String ids) {
        String[] idArray = ids.split(",");
        for (String id : idArray) {
            this.teacherRepository.deleteById(Integer.parseInt(id));
        }
    }
}
